home *** CD-ROM | disk | FTP | other *** search
/ Mac Cube 4: Multimedia Applications / MacCube Volume 4: Multimedia Applications.iso / Graphics / NIH Image Folder / Macros / LUT Macros < prev    next >
Text File  |  1993-08-05  |  8KB  |  376 lines

  1. macro 'Export LUT [E]';
  2. {
  3. Copies the current look-up table to the Area(Red), Mean(Green) and
  4. Perimeter/Length(blue) columns. Max Measurements must be set to
  5. 256 or greater.
  6. }
  7. var
  8.   i:integer;
  9.   v:real;
  10. begin
  11.   RequiresVersion(1.45);
  12.   SetCounter(256); 
  13.   SetOptions('Area,Mean, Perimeter');
  14.   for i:=0 to 255 do begin
  15.     rArea[i+1]:=RedLut[i];
  16.     rMean[i+1]:=GreenLut[i];
  17.     rLength[i+1]:=BlueLut[i];
  18.   end;
  19.   ShowResults;
  20.   SetExport('Measurements');
  21.   Export('RGB LUT');
  22. end;
  23.  
  24.  
  25. macro 'Invert LUT [I]';
  26. var
  27.   i:integer;
  28. begin
  29.   for i:=1 to 254 do begin
  30.     RedLUT[i]:=255-RedLut[i];
  31.     GreenLUT[i]:=255-GreenLut[i];
  32.     BlueLUT[i]:=255-BlueLut[i];
  33.   end;
  34.   UpdateLUT;
  35. end;
  36.  
  37.  
  38. macro 'Log Tranform';
  39. var
  40.   i,v:integer;
  41.   ln255:real;
  42. BEGIN
  43.   RedLUT[255]:=0;
  44.   GreenLUT[255]:=0;
  45.   BlueLUT[255]:=0;
  46.   ln255:=ln(255);
  47.   for i:=1 to 255 DO begin
  48.     v:=round(ln(i)*255.0/ln255);
  49.     RedLUT[255-i]:=v;
  50.     GreenLUT[255-i]:=v;
  51.     BlueLUT[255-i]:=v;
  52.   end;
  53.   UpdateLUT;
  54. END.
  55.  
  56.  
  57. macro 'Gamma Tranform… [G]';
  58. var
  59.   i,v:integer;
  60.   n,mode,min,max:integer
  61.   gamma,mean:real;
  62. begin
  63.   gamma:=GetNumber('Gamma(0.1-3.0):',2);
  64.   measure;
  65.   GetResults(n,mean,mode,min,max);
  66.   ShowMessage('min=',min:1,'\max=',max:1);
  67.   for i:=1 to 254 DO begin
  68.     if (i>min) and (i<max)
  69.       then v:=exp(gamma*ln((i-min)/(max-min)))*255 {x^y=exp(y*ln(x)}
  70.       else begin
  71.         if i<=min then v:=0 else v:=255;
  72.       end;
  73.     RedLUT[i]:=255-v;
  74.     GreenLUT[i]:=255-v;
  75.     BlueLUT[i]:=255-v;
  76.   end;
  77.   UpdateLUT;
  78. end;
  79.  
  80.  
  81. macro 'Square Tranform';
  82. var
  83.   i,v:integer;
  84.   sqr255:real;
  85. BEGIN
  86.   sqr255:=sqr(255.0);
  87.   for i:=1 to 255 DO begin
  88.     v:=round(sqr(i)*255.0/sqr255);
  89.     RedLUT[255-i]:=v;
  90.     GreenLUT[255-i]:=v;
  91.     BlueLUT[255-i]:=v;
  92.   end;
  93.   UpdateLUT;
  94. END.
  95.  
  96.  
  97. macro 'Square Root Tranform';
  98. var
  99.   i,v:integer;
  100.   sqrt255:real;
  101. BEGIN
  102.   sqrt255:=sqrt(255.0);
  103.   for i:=1 to 255 DO begin
  104.     v:=round(sqrt(i)*255.0/sqrt255);
  105.     RedLUT[255-i]:=v;
  106.     GreenLUT[255-i]:=v;
  107.     BlueLUT[255-i]:=v;
  108.   end;
  109.   UpdateLUT;
  110. END;
  111.  
  112.  
  113. macro 'Reset LUT [R]';
  114. begin
  115.   ResetGrayMap;
  116. end;
  117.  
  118.  
  119. macro 'Plot LUT [P]';
  120. var
  121.   i,xscale,yscale:real;
  122.   width,height,margin,pwidth,pheight:integer;
  123.   xbase,ybase:integer;
  124. begin
  125.   SaveState;
  126.   margin:=25;
  127.   pwidth:=400;
  128.   pheight:=125;
  129.   width:=pwidth+2*margin;
  130.   height:=pheight*3+2*margin;
  131.   SetNewSize(width,height);
  132.   SetBackground(0); 
  133.   MakeNewWindow('LUT');
  134.   xscale:=(pwidth-2)/256;
  135.   yscale:=(pheight-1)/256;
  136.   SetForeground(252);
  137.   xbase:=margin; ybase:=margin;
  138.   MoveTo(xbase,ybase);
  139.   for i:=0 to 255 do
  140.     LineTo(xbase+i*xscale,ybase+RedLUT[i]*yscale);
  141.   SetForeground(255);
  142.   MakeRoi(xbase,ybase,pwidth,pheight);
  143.   FlipVertical;
  144.   DrawBoundary;
  145.   SetForeground(253);
  146.   ybase:=ybase+pheight-1;
  147.   MoveTo(xbase,ybase);
  148.   for i:=0 to 255 do
  149.     LineTo(xbase+i*xscale,ybase+GreenLUT[i]*yscale);
  150.   SetForeground(255);
  151.   MakeRoi(xbase,ybase,pwidth,pheight);
  152.   FlipVertical;
  153.   DrawBoundary;
  154.   SetForeground(254);
  155.   ybase:=ybase+pheight-1;
  156.   MoveTo(xbase,ybase);
  157.   for i:=0 to 255 do
  158.     LineTo(xbase+i*xscale,ybase+BlueLUT[i]*yscale);
  159.   SetForeground(255);
  160.   MakeRoi(xbase,ybase,pwidth,pheight);
  161.   FlipVertical;
  162.   DrawBoundary;
  163.   KillRoi;
  164.   RedLUT[252]:=255; GreenLUT[252]:=0;   BlueLUT[252]:=0;
  165.   RedLUT[253]:=0;   GreenLUT[253]:=255; BlueLUT[253]:=0;
  166.   RedLUT[254]:=0;   GreenLUT[254]:=0;   BlueLUT[254]:=255;
  167.   UpdateLUT;
  168.   SetFont('Geneva');
  169.   SetFontSize(9);
  170.   SetText('Centered');
  171.   MoveTo(margin+4,height-margin+8);
  172.   writeln(0:1:2);
  173.   MoveTo(margin+pwidth,height-margin+8);
  174.   writeln(255:1:2);
  175.   RestoreState;
  176. end;
  177.  
  178.  
  179. macro 'Posterize…';
  180. var
  181.   level,i:integer
  182.   delta,steps,StepSize,NextStep:real;
  183. begin
  184.   steps:=GetNumber('Number of Gray Steps(2-256):',8);
  185.   StepSize:=256/steps;
  186.   delta:=256/(steps-1);
  187.   NextStep:=trunc(StepSize);
  188.   level:=255;
  189.   for i:=0 to 255 do begin
  190.     if i>=NextStep then begin
  191.       NextStep:=trunc(NextStep+StepSize);
  192.       level:=level-delta;
  193.       UpdateLUT;
  194.     end;
  195.     if level<0 then level:=0;
  196.     RedLUT[i]:=level;
  197.     GreenLUT[i]:=level;
  198.     BlueLUT[i]:=level;
  199.   end;
  200. end;
  201.  
  202.  
  203. macro 'Make Four Ramp LUT';
  204. var
  205.   i,entry:integer;
  206. BEGIN
  207.   entry:=0;
  208.   for i:=0 to 63 DO begin
  209.     RedLUT[entry]:=255-i*4;
  210.     GreenLUT[entry]:=255-i*4;
  211.     BlueLUT[entry]:=255-i*4;
  212.     entry:=entry+1;
  213.   end;
  214.   for i:=0 to 63 DO begin
  215.     RedLUT[entry]:=255-i*4;
  216.     GreenLUT[entry]:=0;
  217.     BlueLUT[entry]:=0;
  218.     entry:=entry+1;
  219.   end;
  220.    for i:=0 to 63 DO begin
  221.     RedLUT[entry]:=0;
  222.     GreenLUT[entry]:=255-i*4;
  223.     BlueLUT[entry]:=0;
  224.     entry:=entry+1;
  225.   end;
  226.   for i:=0 to 63 DO begin
  227.     RedLUT[entry]:=0;
  228.     GreenLUT[entry]:=0;
  229.     BlueLUT[entry]:=255-i*4;
  230.     entry:=entry+1;
  231.   end;
  232.   UpdateLUT;
  233. end.
  234.  
  235.  
  236. macro 'Set Pixels Red…';
  237. var
  238.  v1,v2,i:integer;
  239. begin
  240.     v1:=GetNumber('Starting Pixel Value(1-254)',10);
  241.     v2:=GetNumber('Ending Pixel Value(1-254)',10);
  242.     if v2<v1 then begin
  243.       PutMessage('Ending value less than starting value.');
  244.       exit;
  245.     end;
  246.     for i:=v1 to v2 do begin
  247.       RedLUT[i]:=255;
  248.       GreenLUT[i]:=0;
  249.       BlueLUT[i]:=0;
  250.     end;
  251.   end;
  252.   UpdateLUT;
  253. end;
  254.  
  255.  
  256. macro 'Nearly Gray LUT…';
  257. {
  258. Here is a macro that changes the LUT to make the values near 128 fairly visible when making polygon and line selections which use XOR drawing mode.
  259. Play around with it to get better results. It was written on the
  260. (incorrect) assumption that brightness = r+g+b.
  261. j is i xor 255 and also white is 255,255,255 not 0,0,0.
  262. {The brightness of each pixel is not quite right, there is a better way to get different colors with same brightness...)
  263. --Edward J. Huff (huff@mcclb0.med.nyu.edu)
  264. }
  265. var
  266.  i,j,d: integer;
  267. begin
  268.    while (d < 1) or (d > 63) do
  269.      d := GetNumber('Amount of color',20);
  270.   for i := d*2 to 127 do begin
  271.      j := 255 - i; 
  272.      RedLUT[i] := j + d;
  273.      GreenLUT[i] := j + d;
  274.      BlueLUT[i] := j - d*2;
  275.      RedLUT[j] := i - d*2;
  276.      GreenLUT[j] := i + d;
  277.      BlueLUT[j] := i + d;
  278.   end;
  279.   UpdateLUT;
  280. end;
  281.  
  282. macro 'Color Merge Two Images';
  283. {
  284. Merges a "red" image and a "green" image to create a
  285. composite color image. The macro does this by scaling both
  286. images to 0-15, multiplying the second by 16, creating a
  287. single 8-bit by ORing the two 4-bit images, and then
  288. generating a custom red and green LUT to display the
  289. composite image.
  290. }
  291. var
  292.   i,w1,w2,h1,h2,merged:integer;
  293. begin
  294.   SaveState;
  295.   if nPics<>2 then begin
  296.     PutMessage('This macro operates on exactly two images.');
  297.     exit;
  298.   end;
  299.   SelectPic(1);
  300.   GetPicSize(w1,h1);
  301.   SelectPic(2);
  302.   GetPicSize(w2,h2);
  303.   if (w1<>w2) or (h1<>h2) then begin
  304.     PutMessage('The two images must have the same width and height.');
  305.     exit;
  306.   end;
  307.   SetNewSize(w1,h2);
  308.   MakeNewWindow('Merged');
  309.   merged:=PicNumber;
  310.   SelectPic(1);
  311.   SelectAll;
  312.   Copy;
  313.   SelectPic(merged);
  314.   Paste;
  315.   SelectAll;
  316.   MultiplyByConstant(1/16);
  317.   ChangeValues(0,0,1);
  318.   ChangeValues(16,16,15);
  319.   SelectPic(2);
  320.   SelectAll;
  321.   Duplicate('Temp');
  322.   MultiplyByConstant(1/16);
  323.   ChangeValues(16,16,15);
  324.   MultiplyByConstant(16);
  325.   ChangeValues(0,0,1);
  326.   SelectAll;
  327.   Copy;
  328.   SelectPic(merged);
  329.   Paste;
  330.   DoOr;
  331.   for i:=0 to 255 do begin
  332.      RedLut[i]:=(i mod 16)*16;
  333.      GreenLut[i]:=(i div 16)*16;
  334.      BlueLut[i]:=0;
  335.    end;
  336.   UpdateLut;
  337.   SelectPic(nPics);
  338.   Dispose;  {Temp}
  339.   RestoreState;
  340. end;
  341.  
  342.  
  343. macro 'Move Slice Up [U]';
  344. var
  345.   lower,upper:integer;
  346. begin
  347.   GetThresholds(lower,upper);
  348.   lower:=lower-1;
  349.   upper:=upper-1;
  350.   if lower<1 then lower:=1;
  351.   if lower>254 then lower:=254;
  352.   if upper<lower then upper:=lower;
  353.   if upper>254 then upper:=254;
  354.   SetDensitySlice(lower,upper);
  355.   ShowMessage(lower:4,upper:4)
  356. end;
  357.  
  358. macro 'Move Slice Down [D]';
  359. var
  360.   lower,upper:integer;
  361. begin
  362.   GetThresholds(lower,upper);
  363.   lower:=lower+1;
  364.   upper:=upper+1;
  365.   if lower<1 then lower:=1;
  366.   if lower>254 then lower:=254;
  367.   if upper<lower then upper:=lower;
  368.   if upper>254 then upper:=254;
  369.   SetDensitySlice(lower,upper);
  370.   ShowMessage(lower:4,upper:4)
  371. end;
  372.  
  373.  
  374.  
  375.  
  376.